
//############Movement gait||运动步态
enum gait {
    //% block="Trot"
    3,
    //% block="F_TROT"
    4,
    //% block="S_TROT"
    5,
    //% block="Crawl"
    6
    
      
}

//############Actions||动作组
enum Choice{
    //% block="0"
    0
    //% block="1"
    1,
    //% block="2"
    2
    
}

//############Actions state||动作组状态
enum Actions{
    //% block="Enable"
    1,
    //% block="Not_Enable"
    0

}


 //############Movement direction||运动方向
enum Mov_dir{
    //% block="Forward"
    0,
    //% block="Backward"
    1,
    //% block="Turn_left"
    2,
    //% block="Turn_right"
    3,
    //% block="Shift_left"
    4,
    //% block="Shift_right"
    5
}

//############Movement Angle||运动角度
enum Mov_ang{
    //% block="Left_swing"
    0,
    //% block="Right_swing"
    1,
    //% block="Look_down"
    2,
    //% block="Look_up"
    3,
    //% block="Yaw_left"
    4,
    //% block="Yaw_right"
    5
}

//############gesture||手势
enum gesture{
    //% block="Right to left"
    1,
    //% block="From left to right"
    2,
    //% block="From top to bottom"
    4,
    //% block="Bottom up"
    8,
    //% block="Back to front"
    16,
    //% block="From front to back"
    32,
    //% block="Clockwise"
    64,
    //% block="Counterclockwise"
    128,
    //% block="Wave"
    256

}

//############Audio control instructions||音频控制指令
enum CMD{
    //% block="back"
    0x01,
    //% block="next"
    0x02,
    //% block="Specifies the track"
    0x03,
    //% block="volume+"
    0x04,
    //% block="volume-"
    0x05,
    //% block="Play"
    0x0D,
    //% block="Pause playback"
    0x0E,
    //% block="Stop playback"
    0x16,
    //% block="The single plays on a loop"
    0x19,
    // //% block="Specifies the volume"
    // 0x06,
    //% block="Loop playback"
    0x08,
    //% block="Enter sleep mode"
    0x0A,
    //% block="Wake up to sleep"
    0x0B,
    //% block="Chip reset"
    0x0C
    

}

//############track||曲目
enum dat{
    //% block="none"
    0x00,
    //% block="welcome"
    0x01,
    //% block="thank"
    0x02,
    //% block="song"
    0x03

}

//% color="#0fbc11" iconWidth=60 iconHeight=50
namespace Quadruped {
    //###Start||初始化
    //% block="initialization" blockType="command" 
    export function SPI_Init(parameter: any, block: any) {
       Generator.addImport("import Robots"); 
       Generator.addCode(`Robots.Init()`); 
   }

    //###Start||启动
    //% block="Start" blockType="command"
    export function Start(parameter: any, block: any) {
        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Robots_mode(0x02)`);   
        Generator.addCode(`time.sleep_ms(100);`);
    }

    //###Stop||停止
    //% block="Stop" blockType="command"
    export function Stop(): void {
        Generator.addImport("import Robots");
        //Generator.addCode(`Robots.Robot_Reset();`);
        //Generator.addCode(`time.sleep_ms(1000);`);
        Generator.addCode(`Robots.Robots_mode(0x07)`);
        Generator.addCode(`time.sleep_ms(1000);`);
        Generator.addCode(`Robots.Robots_mode(0x01)`);  
    }
    
    //####Reset||复位
    //% block="Reset" blockType="command"
    export function Reset(): void{
        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Robot_Reset();`);

    }

    // //###Stand||站立 
    // //% block="Stand" blockType="command"
    // export function Stand(): void{
    //     Generator.addImport("import Robots");
    //     Generator.addCode(`Robots.Robots_mode(0x07)`);  
    // }

    //####Hight||站立的高度
    //% block="Standing height[H]" blockType="command"
    //%H.shadow="range" H.params.min=0 H.params.max=10 H.defl=10
    export function Height(parameter: any, block: any){
        let h = parameter.H.code;

        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Robots_mode(0x07)`); 
        Generator.addCode(`Robots.Robots_High(${h});`); 
       // Generator.addCode(`time.sleep_ms(1700);`);
    }

    //###Gait||步态
    //% block="Gait[G]" blockType="command"
    //% G.shadow="dropdown" G.options="gait" G.defl="gait.Trot"
    export function Gait(parameter: any, block: any) {
        let g = parameter.G.code;

        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Robots_mode(${g});`); 
    }

    //###Action group||动作组
    //% block="Action_group[GROUP]State[STATE]" blockType="command"
    //% GROUP.shadow="dropdown" GROUP.options="Choice" GROUP.defl="Choice.Enable"
    //% STATE.shadow="dropdown" STATE.options="Actions" STATE.defl="Actions.Enable"
    export function Action(parameter: any, block: any): void{
        let group = parameter.GROUP.code;
        let state = parameter.STATE.code;

        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Robots_Action(${group},${state});`); 
    }
    
    //###Before and after movement direction and speed||前后运动方向与速度
    //% block="Control[FOR]Speed[V1]" blockType="command"
    //% FOR.shadow="dropdown" FOR.options="Mov_dir" FOR.defl="Mov_dir.0"
    //% V1.shadow="range" V1.params.min=0 V1.params.max=14 V1.defl=0
    export function movement(parameter: any, block: any) {
        let ffor = parameter.FOR.code;    
        let v1 = parameter.V1.code;
       
        Generator.addImport("import Robots");
        if(ffor == 0){
        Generator.addCode(`Robots.Robots_Xspeed(${v1/10});`);
        }
        else if(ffor == 1){
            Generator.addCode(`Robots.Robots_Xspeed(${v1/(-10)});`);
        }
        else if(ffor == 2){
            Generator.addCode(`Robots.Robots_Zspeed(${v1/10});`);
        }
        else if(ffor == 3){
            Generator.addCode(`Robots.Robots_Zspeed(-${v1/10});`);
        }
        else if(ffor == 4){
            Generator.addCode(`Robots.Robots_Yspeed(-${v1/10});`);
        }
        else if(ffor == 5){
            Generator.addCode(`Robots.Robots_Yspeed(${v1/10});`);
        }
   }

    //###Control angle||控制角度
    //% block="Control[SWAY]angle[ANGLE1]" blockType="command"
    //% SWAY.shadow="dropdown" SWAY.options="Mov_ang" SWAY.defl="Mov_ang.0"
    //% ANGLE1.shadow="range" ANGLE1.params.min=0 ANGLE1.params.max=14 ANGLE1.defl=0
    export function Control_angle(parameter: any, block: any): void{
        let sway = parameter.SWAY.code;    
        let angle1 = parameter.ANGLE1.code;
 
        Generator.addImport("import Robots");
        if(sway == 0){
        Generator.addCode(`Robots.Robots_Yangle(${angle1/10});`);
        }
        else if(sway == 1){
            Generator.addCode(`Robots.Robots_Yangle(-${angle1/10});`);
        }
        else if(sway == 2){
            Generator.addCode(`Robots.Robots_Xangle(${angle1/10});`);
        }
        else if(sway == 3){
            Generator.addCode(`Robots.Robots_Xangle(-${angle1/10});`);
        }
        else if(sway == 4){
            Generator.addCode(`Robots.Robots_Zangle(${angle1/10});`);
        }
        else if(sway == 5){
            Generator.addCode(`Robots.Robots_Zangle(-${angle1/10});`);
        }
    }
    
    // //###Steering gear control||舵机控制
    // //% block="Steering_gear_control the[ge]PWM[num]Speed[v2]millisecond" blockType="command"
    // //% ge.shadow="dropdown" ge.options="PWM" ge.defl="PWM.0"
    // //% num.shadow="range" num.params.min=0 num.params.max=2500 num.defl=0
    // //% v2.shadow="range" v2.params.min=0 v2.params.max=9 v2.defl=0
    // export function Steering_gear(parameter: any, block: any): void{

    // }

    //####Get the data||获取数据
    //% block="Get the data" blockType="command"
    export function get_data(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(`datareturn = bytearray(5);`);
        Generator.addCode(`if datareturn != None:
        datareturn = Robots.Robots_Sta()`);
        //Generator.addCode(`time.sleep_ms(1000);`);
    }

    //####Get the data status||获取数据的类型
    //% block="Get the data status" blockType="reporter"
    export function get_data_status(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(["datareturn", Generator.ORDER_UNARY_POSTFIX]);

    }

    //###Returns action status information||返回动作组状态信息
    //% block="Returns action group status information" blockType="reporter"
    export function Action_Status(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(["datareturn[1]", Generator.ORDER_UNARY_POSTFIX]);
    }

    //###Return  hexadecimal number||返回状态信息
    //% block="Returns the status information of the robot itself" blockType="reporter"
    export function Status(block: any): void{
        Generator.addImport("import Robots");
        //Generator.addCode(`if datareturn != None:`);
        Generator.addCode(["datareturn[0]", Generator.ORDER_UNARY_POSTFIX]);

    }

    //###手势初始化
    //% block="Gesture configuration settings" blockType="reporter"
    export function Gesture_init(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(`Robots.i2cscan_gesture()`);
    }

    //###获取手势值
    //% block="Returns the value of the gesture direction" blockType="reporter"
    export function Get_Gesture(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(`Robots.GetGesture()`);
    }

    //###选择手势值
    //% block="A value that defines the direction of the gesture[GESTURE]" blockType="reporter"
    //% GESTURE.shadow="dropdown" GESTURE.options="gesture" GESTURE.defl="gesture.1"
    export function Gesture_type(parameter: any,block: any): void{
        let gesture = parameter.GESTURE.code;

        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Select_gesture_as(${gesture})`);
    }

    //####Ball tracking||小球跟踪
    //% block="Ball_tracking" blockType="command"
    export function Ball_Tracking(parameter: any, block: any):void{
        Generator.addImport("import Robots");
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Ball_Tracking(Robots.IdentifyLine(img))`);
    }

    //###超声波测距
    //% block="Returns ultrasound data" blockType="reporter"
    export function Ultrasonic_ranging(block: any): void{
        Generator.addImport("import Robots");
        Generator.addCode(`Robots.ultrasonic_init(0x57)`);
    }

    //###巡线
    //% block="Hunt following" blockType="command"
    export function Hunt_following(block: any): void{
        Generator.addImport("import Robots");
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Line_patrol(Robots.Line_distinguish(img))`);
    }

    //###音频控制
    //% block="Play music[CM]Song selection[DA]" blockType="command"
    //% CM.shadow="dropdown" CM.options="CMD" CM.defl="CMD.0x03"
    //% DA.shadow="dropdown" DA.options="dat" DA.defl="DA.0x00"
    export function Audio_control(parameter: any, block: any): void{
        let CMD = parameter.CM.code;
        let dat = parameter.DA.code;

        Generator.addImport("import Robots");
        Generator.addCode(`Robots.Audio_ontrol(${CMD},0,${dat})`);
    }
}
